package com.neusoft.neuiotms.dcvm.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcvm.service.DatabusService;
import com.neusoft.neuiotms.dcvm.service.StatisticService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("统计管理")
@RestController
@RequestMapping(value = "/api/w/statistics")
public class StatisticController {

    @Autowired
    private StatisticService statisticService;
    @Autowired
    private DatabusService databusService;

    @ApiOperation("模具总数")
    @GetMapping("/model/count")
    public ResultDTO<?> countOfModels() {
        final long count = this.statisticService.countOfModels();
        return ResultDTO.success(count);
    }

    @ApiOperation("位置数量")
    @GetMapping("/location/count")
    public ResultDTO<?> countOfLocations() {
        final long count = this.statisticService.countOfLocations();
        return ResultDTO.success(count);
    }

    @ApiOperation("模型数量")
    @GetMapping("/schema/count")
    public ResultDTO<?> countOfSchemas() {
        final long count = this.statisticService.countOfSchemas();
        return ResultDTO.success(count);
    }

    @ApiOperation("资产总数")
    @GetMapping("/asset/count")
    public ResultDTO<?> countOfAssets() {
        final long count = this.statisticService.countOfAssets();
        return ResultDTO.success(count);
    }

    @ApiOperation("IT资产总数")
    @GetMapping("/itasset/count")
    public ResultDTO<?> countOfItAssets() {
        final long count = this.statisticService.countOfItAssets();
        return ResultDTO.success(count);
    }

    @ApiOperation("配电柜总数")
    @GetMapping("/pdg/count")
    public ResultDTO<?> countOfPdgs() {
        final long count = this.statisticService.countOfPdgs();
        return ResultDTO.success(count);
    }

    @ApiOperation("机柜使用率统计")
    @GetMapping("/rack/{PosCode}")
    public ResultDTO<?> rack(@PathVariable final String PosCode) {
        final JSONObject result = this.statisticService.rack(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("机柜类型统计")
    @GetMapping("/rackType/{PosCode}")
    public ResultDTO<?> rackType(@PathVariable final String PosCode) {
        final JSONObject result = this.statisticService.rackType(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("机柜U位统计")
    @GetMapping("/u/{PosCode}")
    public ResultDTO<?> u(@PathVariable final String PosCode) {
        final JSONObject result = this.statisticService.u(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("数据中心网络流量统计")
    @GetMapping("/netflow/{PosCode}")
    public ResultDTO<?> netflow(@PathVariable final String PosCode,
            @RequestParam(required = false) Map<String, Object> params) {
        final Map<String, Object> res = this.databusService.fetchMonitorData(PosCode, params);
        return ResultDTO.success(res);
    }

    @ApiOperation("机房租赁KPI统计")
    @GetMapping("/leaseBusiness/kpi/{PosCode}")
    public ResultDTO<?> getLBKpi(@PathVariable final String PosCode) {
        final JSONArray result = this.statisticService.getLBKpi(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("机房机柜占用状态统计")
    @GetMapping("/leaseBusiness/rackStatus/{PosCode}")
    public ResultDTO<?> getLBRackStatus(@PathVariable final String PosCode) {
        final JSONArray result = this.statisticService.getLBRackStatus(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("机房U位使用状态统计")
    @GetMapping("/leaseBusiness/uStatus/{PosCode}")
    public ResultDTO<?> getLBUStatus(@PathVariable final String PosCode) {
        final JSONArray result = this.statisticService.getLBUStatus(PosCode);
        if (result == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("根据位置编码和业务类型名称获取客户数及业务数量")
    @GetMapping("/leaseBusiness/custAndIndent/{PosCode}/{RackTypeName}")
    public ResultDTO<?> getLBCustAndIndent(@PathVariable final String PosCode,
            @PathVariable final String RackTypeName) {
        final JSONArray result = this.statisticService.getLBCustAndIndent(PosCode, RackTypeName);
        if (result == null) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(result);
    }

    @ApiOperation("根据位置编码获取客户数及业务数量")
    @GetMapping("/leaseBusiness/custAndIndent/{PosCode}")
    public ResultDTO<?> getLBCustAndIndentWithoutRackType(@PathVariable final String PosCode) {
        final JSONArray result = this.statisticService.getLBCustAndIndent(PosCode, null);
        if (result == null) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(result);
    }

}
